package com.richapp.launcher.scene.conditions.sensor;

import com.richapp.launcher.database.system.dao.ConditionSensorModelDao;
import com.richapp.launcher.database.system.dao.SceneModelDao;
import com.richapp.launcher.database.system.mode.ConditionSensorModel;
import com.richapp.launcher.database.system.mode.DeviceModel;
import com.richapp.launcher.database.system.mode.SceneModel;
import com.richapp.launcher.scene.Common;
import com.richapp.launcher.scene.SceneServer;
import com.richapp.launcher.scene.abstracts.SceneAbstractSensorConditions;
import com.richapp.launcher.scene.interfaces.ISceneConditions;
import com.richapp.launcher.scene.interfaces.ISceneSensorConditions;
import com.richapp.net.util.HexTool;

public class SceneSensorPm25Conditions extends SceneAbstractSensorConditions {


    private int checkPM25Value;


    public SceneSensorPm25Conditions() {

    }

    @Override
    public int getConditionType() {
        return ISceneConditions.SCENE_CONDITION_TYPE_SENSOR;
    }

    @Override
    public int getConditionSubType() {
        return ISceneSensorConditions.SCENE_CONDITION_SUB_TYPE_SENSOR_PM25;
    }

    @Override
    public boolean checkCondition(String sceneName) {
        boolean checkFlag=false;
        DeviceModel device = getDevice();
        SceneModel sceneModel = new SceneModel();
        SceneModelDao sceneModelDao = new SceneModelDao(SceneServer.getInstance().getContext());
        if (sceneName.equals("")){
            return checkFlag;
        }
        sceneModel=sceneModelDao.getSceneModelOnly(sceneName);
        if (device != null) {
            int value = device.getPm25();
            switch (getConditionCheckModel()[0]) {
                case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_GREATERTHAN:
                    if (value > checkPM25Value && "true".equals(sceneModel.getStatusCycleFlag())) {
                        checkFlag = true;
                    }else if (value <= checkPM25Value){
                        sceneModel.setStatusCycleFlag("true");
                        sceneModelDao.add(sceneModel);
                    }
                    break;

                case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_EQUALS:
                    if (value == checkPM25Value  && "true".equals(sceneModel.getStatusCycleFlag())) {
                        checkFlag = true;
                    }else if (value != checkPM25Value){
                        sceneModel.setStatusCycleFlag("true");
                        sceneModelDao.add(sceneModel);
                    }
                    break;

                case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_LESSTHAN:
                    if (value < checkPM25Value  && "true".equals(sceneModel.getStatusCycleFlag())) {
                        checkFlag = true;
                    }else if (value >= checkPM25Value){
                        sceneModel.setStatusCycleFlag("true");
                        sceneModelDao.add(sceneModel);
                    }
                    break;

                default:
                    break;
            }
        }

        return checkFlag;
    }

    public int getCheckPM25Value() {
        return checkPM25Value;
    }

    public void setCheckPM25Value(int checkPM25Value) {
        this.checkPM25Value = checkPM25Value;
    }

}
